package org.ry.camera.wxapi;
import org.ry.camera.R;
import org.ry.camera.activity.BaseActivity;
import org.ry.camera.activity.PhotoActivity;
import org.ry.camera.app.CameraApplication;
import org.ry.camera.tools.LogTool;
import org.ry.camera.tools.SharedSave;
import org.ry.camera.tools.Util;

import com.tencent.mm.sdk.openapi.BaseReq;
import com.tencent.mm.sdk.openapi.BaseResp;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.sdk.openapi.SendMessageToWX;
import com.tencent.mm.sdk.openapi.WXAPIFactory;
import com.tencent.mm.sdk.openapi.WXImageObject;
import com.tencent.mm.sdk.openapi.WXMediaMessage;
import com.tencent.mm.sdk.openapi.WXTextObject;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

public class WXEntryActivity extends Activity implements IWXAPIEventHandler
,OnClickListener{

	private IWXAPI mIWXAPI;
	@Override
	public void onReq(BaseReq arg0) {
		// TODO Auto-generated method stub
		LogTool.i("WeinXin:onReq");
	}

	@Override
	public void onResp(BaseResp resp) {
		// TODO Auto-generated method stub
		LogTool.i("WeinXin:onResp");
		int result = 0;
		switch (resp.errCode) {
		case BaseResp.ErrCode.ERR_OK:
			result = R.string.errcode_success;
			break;
		case BaseResp.ErrCode.ERR_USER_CANCEL:
			result = R.string.errcode_cancel;
			break;
		case BaseResp.ErrCode.ERR_AUTH_DENIED:
			result = R.string.errcode_deny;
			break;
		default:
			result = R.string.errcode_unknown;
			break;
		}

		Toast.makeText(this, result, Toast.LENGTH_LONG).show();
		
		/*Intent mIntent = new Intent(WXEntryActivity.this,PhotoActivity.class);
		startActivity(mIntent);
		finish();*/
	}

	private void sendWeiXinMessage(){


		Bitmap bmp = getBitmapObj();
		WXImageObject imgObj = new WXImageObject(bmp);

		WXMediaMessage msg = new WXMediaMessage();
		msg.mediaObject = imgObj;

		Bitmap thumbBmp = Bitmap.createScaledBitmap(bmp, CameraApplication.THUMB_SIZE, CameraApplication.THUMB_SIZE, true);
		//bmp.recycle();
		msg.thumbData = Util.bmpToByteArray(thumbBmp, true);

		SendMessageToWX.Req req = new SendMessageToWX.Req();
		req.transaction = CameraApplication.buildTransaction("img");
		req.message = msg;
		req.scene = SendMessageToWX.Req.WXSceneTimeline;
		mIWXAPI.sendReq(req);
		
		LogTool.i("WeinXin:Send");
		
		finish();
	}


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_wechat);
		
		LogTool.i("WXEntryActivity:initDate");
		
		mIWXAPI = WXAPIFactory.createWXAPI(this,CameraApplication.WINXIN_APP_KEY, false);

		mIWXAPI.handleIntent(getIntent(), this);
		
		int wxSdkVersion = mIWXAPI.getWXAppSupportAPI();
		if (wxSdkVersion >= CameraApplication.TIMELINE_SUPPORTED_VERSION) {
			mIWXAPI.registerApp(CameraApplication.WINXIN_APP_KEY);  
			sendWeiXinMessage();
		}else{
			Toast.makeText(WXEntryActivity.this, getString(R.string.no_support_wechat), Toast.LENGTH_LONG).show();
		}
		
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		
	}
	
	private Bitmap getBitmapObj(){
		return SharedSave.getBitmap(WXEntryActivity.this, CameraApplication.CACHE_BITMAP);
	}
	
	@Override
	protected void onNewIntent(Intent intent) {
		super.onNewIntent(intent);
		LogTool.i("onNewIntent");
		setIntent(intent);
		mIWXAPI.handleIntent(intent, this);
	}

	
}
